package com.iafme.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.RulesReqDTO;
import com.iafme.dto.res.RulesResDTO;
import com.iafme.model.Rules;
import com.iafme.service.RulesService;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author summer
 * @version 1.0
 * @date 2025/4/3 10:21
 */
@RestController
@RequestMapping(value = "/rules")
@AllArgsConstructor
public class RulesController implements BaseCommonInterface<RulesReqDTO, RulesResDTO> {

    private final RulesService rulesService;

    @Override
    public Result<String> save(@Valid RulesReqDTO rulesReqDTO) {
        rulesService.save(Convert.convert(Rules.class,rulesReqDTO));
        return Result.successResult();
    }

    @Override
    public Result<String> update(@Valid RulesReqDTO rulesReqDTO) {
        rulesService.updateById(Convert.convert(Rules.class,rulesReqDTO));
        return Result.successResult();
    }

    @Override
    public Result<String> del(Integer id) {
        rulesService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<RulesResDTO>> lists(RulesReqDTO rulesReqDTO) {
        LambdaQueryWrapper<Rules> like = Wrappers.lambdaQuery(Rules.class).like(StrUtil.isNotEmpty(rulesReqDTO.getName()), Rules::getName, rulesReqDTO.getName());
        Page<Rules> page=new Page<>(rulesReqDTO.getPageNum(),rulesReqDTO.getPageSize());
        return Result.successResult(Convert.convert(new TypeReference<Page<RulesResDTO>>() {
        },rulesService.page(page, like)));
    }

    @Override
    public Result<RulesResDTO> findById(Integer integer) {
        return null;
    }

    @Override
    public Result<String> batchDel(List<RulesResDTO> list) {
        return null;
    }
}
